package com.blueocean_health.care.management.controller;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.common.excel.ExcelUtil;
import com.blueocean_health.care.management.domain.base.CashRecord;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.base.SysResultByPage;
import com.blueocean_health.care.management.domain.dto.CashRecordTableDto;
import com.blueocean_health.care.management.domain.dto.PullDownDto;
import com.blueocean_health.care.management.service.CashRecordService;
import com.github.pagehelper.PageInfo;

/**
 * 资金管理-日记账
 * @author wuyanli
 *
 */
@RestController
@RequestMapping("web/cash")
public class CashRecordController {
	@Resource
	private CashRecordService cashService;
	
	/**
	 * 存款日记账账号下拉列表 2018年8月20日13:51:26
	 * @return
	 */
	@RequestMapping("account/list")
	public SysResult accountList(){
		List<PullDownDto> accountList = cashService.accountList();
		return SysResult.ok(accountList);
	}
	
	
	/**
	 * 校验银行流水号唯一性  2017年11月30日16:54:34
	 * 已不用  2018年3月22日15:30:23
	 * @param map
	 * @return
	 */
//	@RequestMapping("checkNum")
	public SysResult checkRecipeNum(@RequestBody Map<String,String> map){
		List<CashRecord> receNumList = cashService.getCashRecordByReceNum(map);
		if(StringUtils.isEmpty(receNumList)){
			return SysResult.ok(0, "success");
		}else if(receNumList.size() > 0){
			return SysResult.error(1, "该银行订单流水号已存在");
		}
		return SysResult.ok(0, "success");
	}

	/**
	 * 添加日记账记录  2017年8月22日10:20:10
	 * @param cashRecord
	 * @return
	 */
	@RequestMapping("add")
	public SysResult addCashRecord(@RequestBody CashRecord cashRecord){
		int addCashRecord = cashService.addCashRecord(cashRecord);
		if(addCashRecord <= 0){
			return SysResult.error(1, "error");
		}else{
			return SysResult.ok(0, "success");
			
		}
//		else if(addCashRecord == 999999){
//			return SysResult.error(2, "该银行订单流水号已存在");
//		}
		
	}
	/**
	 * 修改日记账记录  2017年8月22日10:22:36
	 * @param cashRecord
	 * @return
	 */
	@RequestMapping("modify")
	public SysResult updateCashRecord(@RequestBody CashRecord cashRecord){
		int cashRecordById = cashService.updateCashRecordById(cashRecord);
		if(cashRecordById <= 0){
			return SysResult.error(1, "error");
		}else{
			return SysResult.ok(0, "success");
			
		}
//		else if(cashRecordById == 999999){
//			return SysResult.error(2, "该银行订单流水号已存在");
//		}
	}
	
	/**
	 * 根据Id进行数据删除（逻辑删除）  2017年8月22日10:30:50
	 * @param id
	 * @return
	 */
	@RequestMapping("delete")
	public SysResult deleteCashRecordById(@RequestBody Map<String,Integer> map){
		int deleteCashRecordById = cashService.deleteCashRecordById(map.get("id"));
		if(deleteCashRecordById <= 0){
			return SysResult.error(1, "error");
		}
		return SysResult.ok(0, "success");
	}
	
	/**
	 * 根据条件进行查询  2017年8月22日10:30:45
	 * @param map
	 * @return
	 */
	@RequestMapping("list")
	public SysResult getAllCashRecord(@RequestBody Map<String,Object> map){
		
		PageInfo<CashRecord> allCashRecord = cashService.getAllCashRecord(map);
		
		return SysResultByPage.ok(0, "success", allCashRecord.getList(), allCashRecord.getTotal());
	}
	/**
	 * 根据发生日期范围筛选 现金日记账余额表  2017年8月23日11:39:21
	 * @param map
	 * @return
	 */
	@RequestMapping("balance")
	public SysResult getCashBalanceTable(@RequestBody Map<String,String> map, HttpServletResponse response){
		
		List<CashRecordTableDto> cashBalanceTable = cashService.getCashBalanceTable(map);
		
		return SysResult.ok(0, "success", cashBalanceTable);
	}
	/**
	 * 导出现金日记账余额表 Excel  2017年8月25日11:31:35
	 * @param map
	 * @return
	 */
	@RequestMapping("balanceExcel")
	public void getCashBalanceExcel(HttpServletRequest request,HttpServletResponse response){
		try{
			Map<String,String> map = new HashMap<>();
			String startTime = request.getParameter("startTime");
			String endTime = request.getParameter("endTime");
			map.put("startTime", startTime);
			map.put("endTime", endTime);
			List<CashRecordTableDto> cashBalanceTable = cashService.getCashBalanceTable(map);
			String finalFileName = URLEncoder.encode("现金日记账余额表_"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls", "UTF8");
			response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			
			ExcelUtil.getInstance().exportObj2Excel(toClient, cashBalanceTable, CashRecordTableDto.class);
		}catch(IOException e){
			e.printStackTrace();
		}
		
	}
	/**
	 * 根据Id 查询日记账信息 2017年8月23日14:17:16
	 * @param map
	 * @return
	 */
	@RequestMapping("query")
	public SysResult getCashRecordById(@RequestBody Map<String,Integer> map){
		CashRecord cashRecordById = cashService.getCashRecordById(map.get("id"));
		return SysResult.ok(0, "success", cashRecordById);
	}
	
}
